package com.pro.controller;

import com.pro.pojo.JsBeforeUser;
import com.pro.pojo.JsUserCoupon;
import com.pro.pojo.JsUserCouponUsed;
import com.pro.pojo.JsonData;
import com.pro.service.JsUserCouponService;
import com.pro.utils.DateUtils;
import com.pro.utils.FastDFSClientUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 *
 * @author MYH - SiYun
 * @date 2022/1/9 20:31
 * 优惠券控制层
 */
@RequestMapping("/JsUserCoupon")
@Controller
public class JsUserCouponController {

    @Autowired
    private JsUserCouponService jsUserCouponService;
    @Autowired
    private FastDFSClientUtil fastDFSClientUtil;

    /**
     *
     * @author MYH - SiYun
     * @date 2022/1/9 20:34
     * 获取优惠券列表
     */
    @RequestMapping("/getJsUserCouponList")
    @ResponseBody
    public JsonData getJsUserCouponList(JsUserCoupon jsUserCoupon){
        List<JsUserCoupon> jsUserCouponList = jsUserCouponService.getJsUserCouponList(jsUserCoupon);
        return jsUserCouponList != null ?JsonData.buildSuccess(jsUserCouponList):JsonData.buildError("服务器出错了");
    }

    /**
     *
     * @author MYH - SiYun
     * @date 2022/1/10 11:44
     * 优惠券高级检索
     */
    @RequestMapping("/getJsUserCouponRetrieval")
    @ResponseBody
    public JsonData getJsUserCouponRetrieval(JsUserCoupon jsUserCoupon){
        List<JsUserCoupon> jsUserCouponList = jsUserCouponService.getJsUserCouponRetrieval(jsUserCoupon);
        return jsUserCouponList != null ?JsonData.buildSuccess(jsUserCouponList):JsonData.buildError("服务器出错了");
    }

    /**
     *
     * @author MYH - SiYun
     * @date 2022/1/10 11:53
     * 修改优惠券上线与下线状态
     */
    @RequestMapping("/updateJsUserCouponOnline")
    @ResponseBody
    public JsonData updateJsUserCouponOnline(JsUserCoupon jsUserCoupon){
        boolean updateJsUserCouponOnline = jsUserCouponService.updateJsUserCouponOnline(jsUserCoupon);
        return updateJsUserCouponOnline ?JsonData.buildSuccess(updateJsUserCouponOnline):JsonData.buildError("服务器出错了");
    }

    /**
     *
     * @author MYH - SiYun
     * @date 2022/1/10 13:42
     * 删除优惠券
     */
    @RequestMapping("/deleteJsUserCoupon")
    @ResponseBody
    public JsonData deleteJsUserCoupon(Integer couponId){
        boolean deleteJsUserCoupon = jsUserCouponService.deleteJsUserCoupon(couponId);
        return deleteJsUserCoupon?JsonData.buildSuccess(deleteJsUserCoupon):JsonData.buildError("服务器出错了");
    }

    /**
     *
     * @author MYH - SiYun
     * @date 2022/1/10 15:34
     * 查看用户优惠券
     */
    @RequestMapping("/getJsUserCoupon")
    @ResponseBody
    public JsonData getJsUserCoupon(JsBeforeUser jsBeforeUser) {
        List<JsBeforeUser> jsUserCoupon = jsUserCouponService.getJsUserCoupon(jsBeforeUser);
        return jsUserCoupon != null ? JsonData.buildSuccess(jsUserCoupon) : JsonData.buildError("获取失败");
    }

    /**
     *
     * @author MYH - SiYun
     * @date 2022/1/10 15:58
     * 根据用户id查看用户已使用、未使用优惠券
     */
    @RequestMapping("/getJsUserCouponState")
    @ResponseBody
    public JsonData getJsUserCouponState(Integer usedState, Integer userId){
        Integer jsUserCouponState = jsUserCouponService.getJsUserCouponState(usedState, userId);
        return jsUserCouponState!=null?JsonData.buildSuccess(jsUserCouponState):JsonData.buildError("服务器出错了");
    }

    /**
     *
     * @author MYH - SiYun
     * @date 2022/1/10 18:48
     * 查询用户个人过期优惠券
     */
    @RequestMapping("/getJsUserCouponEnd")
    @ResponseBody
    public JsonData getJsUserCouponEnd(Integer userId) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" );
        List<String> jsUserCouponEnd = jsUserCouponService.getJsUserCouponEnd(userId);
        Integer count=0;
        for (String s : jsUserCouponEnd) {
            Date parse = sdf.parse(s);
            if(parse.getTime()<new Date().getTime()){
                count++;
            }
        }
        return count!=null?JsonData.buildSuccess(count):JsonData.buildError("服务器出错了");
    }

    /**
     *
     * @author MYH - SiYun
     * @date 2022/1/10 20:10
     * 给用户赠送优惠券后添加优惠券使用记录
     */
    @RequestMapping("/addJsUserCouponUsed")
    @ResponseBody
    public JsonData addJsUserCouponUsed(JsUserCouponUsed jsUserCouponUsed){
        boolean addJsUserCouponUsed = jsUserCouponService.addJsUserCouponUsed(jsUserCouponUsed);
        return addJsUserCouponUsed?JsonData.buildSuccess(addJsUserCouponUsed):JsonData.buildError("服务器出错了");
    }

    /**
     *
     * @author MYH - SiYun
     * @date 2022/1/10 20:10
     * 给用户赠送优惠券后修改优惠券的剩余数量
     */
    @RequestMapping("/updateJsUserCouponUsed")
    @ResponseBody
    public JsonData updateJsUserCouponUsed(Integer couponId){
        boolean updateJsUserCouponUsed = jsUserCouponService.updateJsUserCouponUsed(couponId);
        return updateJsUserCouponUsed?JsonData.buildSuccess(updateJsUserCouponUsed):JsonData.buildError("服务器出错了");
    }

    /**
     *
     * @author MYH - SiYun
     * @date 2022/1/10 20:36
     * 查询优惠券剩余数量
     */
    @RequestMapping("/getJsUserCouponSurplus")
    @ResponseBody
    public JsonData getJsUserCouponSurplus(Integer couponId){
        Integer jsUserCouponSurplus = jsUserCouponService.getJsUserCouponSurplus(couponId);
        return jsUserCouponSurplus!=null?JsonData.buildSuccess(jsUserCouponSurplus):JsonData.buildError("服务器出错了");
    }

    /**
     *
     * @author MYH - SiYun
     * @date 2022/1/11 10:39
     * 添加优惠券
     */
    @RequestMapping("/addJsUserCoupon")
    @ResponseBody
    public JsonData addJsUserCoupon(JsUserCoupon jsUserCoupon){
        jsUserCoupon.setCreateDate(DateUtils.tranferDateTimeToString(new Date()));
        boolean addJsUserCoupon = jsUserCouponService.addJsUserCoupon(jsUserCoupon);
        return addJsUserCoupon?JsonData.buildSuccess(jsUserCoupon):JsonData.buildError("服务器出错了");
    }

    /**
     *
     * @author MYH - SiYun
     * @date 2022/1/11 13:38
     * 获取所有优惠券编号
     */
    @RequestMapping("/getJsUserCouponCode")
    @ResponseBody
    public JsonData getJsUserCouponCode(){
        List<Integer> jsUserCouponCode = jsUserCouponService.getJsUserCouponCode();
        return jsUserCouponCode!=null?JsonData.buildSuccess(jsUserCouponCode):JsonData.buildError("服务器出错了");
    }

    /**
     *
     * @author MYH - SiYun
     * @date 2022/1/14 8:47
     * 查看优惠券是否过期
     */
    @RequestMapping("/getJsUserCouponEndDate")
    @ResponseBody
    public JsonData getJsUserCouponEndDate(Integer couponId) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" );
        String getJsUserCouponEndDate = jsUserCouponService.getJsUserCouponEndDate(couponId);
        Integer count=0;
        if(sdf.parse(getJsUserCouponEndDate).getTime()<new Date().getTime()){
            count++;
        }
        return count!=null?JsonData.buildSuccess(count):JsonData.buildError("服务器出错了");
    }
    /**
     * @Author 祖铭泽
     * @Date 2022/2/21/0014
     * @description 上传优惠卷图片
     */
    @RequestMapping(value = "/uploadCouponImage")
    @ResponseBody
    public JsonData uploadCouponImage(MultipartFile couponImage){
        if (couponImage.getSize() > 0){
            //数据库文件路径
            String imageUrl = null;
            try {
                imageUrl = fastDFSClientUtil.uploadFile(couponImage);
            } catch (IOException e) {
                e.printStackTrace();
            }
            //前台传递路径
            HashMap<String, Object> map = new HashMap<>();
            String src = fastDFSClientUtil.getResAccessUrl(imageUrl);
            map.put("src", src);
            map.put("code", 200);
            return map.get("src") != null ? JsonData.buildSuccess(map) : JsonData.buildError("上传失败");
        }
        return null;
    }

}
